package com.cn.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.cn.bean.EnumState;
import com.cn.bean.Provider;
import com.cn.dao.AdminDAO;

public class AdminDAOImpl extends  BaseHibernateDAOImpl implements AdminDAO{

	public List getAllProviderByAdmin() {
		try {
			String hql = "from Provider as provider  order by rank";
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(hql);
			List<Object> list = queryObject.list();
			getSession().getTransaction().commit();
			return list;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	public String acceptProvider(int pro_id)
	{
		try {
			String hql = "from Provider as provider where id='"+pro_id+"'";
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(hql);
			List<Object> list = queryObject.list();
			getSession().getTransaction().commit();
			if(list.get(0)==null)
			return "fail";
			else{
				Provider provider=(Provider)list.get(0);
				provider.setState(EnumState.STATE_PASSED);
				update(provider);
			return "success";
			}
		} catch (RuntimeException re) {
			throw re;
		}
	}
	public String rejectProvider(int pro_id)
	{
		try {
			String hql = "from Provider as provider where id='"+pro_id+"'";
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(hql);
			List<Object> list = queryObject.list();
			getSession().getTransaction().commit();
			if(list.get(0)==null)
			return "fail";
			else{
				Provider provider=(Provider)list.get(0);
				provider.setState(EnumState.STATE_FAILED);
				update(provider);
			return "success";
			}
		} catch (RuntimeException re) {
			throw re;
		}
	}
	public String banProvider(int pro_id)
	{
		try {
			String hql = "from Provider as provider where id='"+pro_id+"'";
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(hql);
			List<Object> list = queryObject.list();
			getSession().getTransaction().commit();
			if(list.get(0)==null)
			return "fail";
			else{
				Provider provider=(Provider)list.get(0);
				delete(provider);
				return "success";
			}
		} catch (RuntimeException re) {
			throw re;
		}
	}
}
